﻿using System;
using System.IO;
using System.Xml;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using PlanningGIS.DataConvert.model;
using PlanningGIS.Util;
using PlanningGIS.Win.Controls;
using PlanningGIS.Framework;
using PlanningGIS.Util.Collections;
using PlanningGIS.Util.IO;
using PlanningGIS.Util.Convertor;
using PlanningGIS.Util.Security;
using PlanningGIS.Framework.Rule;
using PlanningGIS.Util.Log;
using PlanningGIS.Framework.Config;

namespace PlanningGIS.DataConvert.form
{
    /// <summary>
    /// 作用：建库方案管理界面
    /// </summary>
    public partial class FrmDataImportRule : Office2007Form
    {
        private int featClsIndex;
        private int ruleIndex;

        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmDataImportRule()
        {
            InitializeComponent();

            tbcWorkspace.BringToFront();

            tbiPipeInfo.Visible = false;
            //if (tbcWorkspace.Controls.Contains(tbpPipe)) tbcWorkspace.Controls.Remove(tbpPipe);

            InitTreeViewDataImportRule(trvRule);

            this.dgvxBasicFc.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(ControlHelper.dataGridViewX_DataError);
            //this.dgvxBasicWs.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(ControlHelper.dataGridViewX_DataError);
            this.dgvxFcode.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(ControlHelper.dataGridViewX_DataError);
            this.dgvxField.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(ControlHelper.dataGridViewX_DataError);
            this.dgvxPipe.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(ControlHelper.dataGridViewX_DataError);

        }

        /// <summary>
        /// 初始化树节点
        /// </summary>
        /// <param name="vTreeView"></param>
        private void InitTreeViewDataImportRule(TreeView vTreeView)
        {
            TreeNode pTreeNodeRt;
            TreeNode pTreeNodeWs;
            TreeNode pTreeNodeFc;
            vTreeView.Nodes.Clear();

            pTreeNodeRt = vTreeView.Nodes.Add("dataImport", "建库规则", "rule","rule");
            foreach (DataImportRule dataImportRule in RuleConfig.DataBuilding.BuildingRules)
            {
                pTreeNodeWs =
                    pTreeNodeRt.Nodes.Add(dataImportRule.RuleName, dataImportRule.RuleName, "workspace");
                pTreeNodeWs.Tag = dataImportRule;
                foreach (DlgTargetFeatureClass dlgTargetFeatureClass in dataImportRule.dlgTargetFeatureClasses)
                {
                    pTreeNodeFc =
                        pTreeNodeWs.Nodes.Add( dlgTargetFeatureClass.FeatureClassAliasName, dlgTargetFeatureClass.FeatureClassAliasName, "featureclass",
                                              "featureclass");
                    pTreeNodeFc.Tag = dlgTargetFeatureClass;
                }
            }

            //pTreeNodeRt = vTreeView.Nodes.Add("dataMatch", "按图层入库", "rule", "rule");
            //foreach (DataMatchRule dataMatchRule in RuleConfig.rulesXmlContainer.dataMatchtRules)
            //{
            //    pTreeNodeWs =
            //        pTreeNodeRt.Nodes.Add(dataMatchRule.RuleName, dataMatchRule.RuleName, "workspace");
            //    pTreeNodeWs.Tag = dataMatchRule;
            //    foreach (DlgTargetMatchFeatureClass dlgTargetMatchFeatureClass in dataMatchRule.dlgTargetMatchFeatureClass)
            //    {
            //        //pTreeNodeFc =
            //        //    pTreeNodeWs.Nodes.Add(dlgTargetMatchFeatureClass.FeatureClassAliasName, dlgTargetMatchFeatureClass.FeatureClassAliasName, "featureclass",
            //        //                          "featureclass");
            //        pTreeNodeFc =
            //                pTreeNodeWs.Nodes.Add(dlgTargetMatchFeatureClass.FeatureClassName, dlgTargetMatchFeatureClass.FeatureClassName, "featureclass",
            //              "featureclass");
            //        pTreeNodeFc.Tag = dlgTargetMatchFeatureClass;
            //    }
            //}

            //pTreeNodeRt = vTreeView.Nodes.Add("pipeImport", "管线入库", "rule", "rule");
            //foreach (PipeImportRule pipeImportRule in RuleConfig.rulesXmlContainer.pipeImportRules)
            //{
            //    pTreeNodeWs =
            //        pTreeNodeRt.Nodes.Add(pipeImportRule.RuleName, pipeImportRule.RuleName, "workspace");
            //    pTreeNodeWs.Tag = pipeImportRule;
            //    foreach (DlgTargetPipeFeatureClass dlgTargetPipeFeatureClass in pipeImportRule.dlgTargetPipeFeatureClasses)
            //    {
            //        pTreeNodeFc =
            //            pTreeNodeWs.Nodes.Add(dlgTargetPipeFeatureClass.FeatureClassAliasName, dlgTargetPipeFeatureClass.FeatureClassAliasName, "featureclass",
            //                                  "featureclass");
            //        pTreeNodeFc.Tag = dlgTargetPipeFeatureClass;
            //    }
            //}
        }

        /// <summary>
        /// 点击规则树节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trvRule_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node == null) return;

            switch (e.Node.ImageKey)
            {
                case "rule" :

                    //右键菜单
                    if (e.Button == MouseButtons.Right)
                    {
                        ctxMenuRule.Show(trvRule, e.X, e.Y);
                    }
                    break;

                case "workspace":
                    tbcWorkspace.BringToFront();
                    ReFreshDataGridWs(e.Node.Tag as DataImportRuleBase);

                    //右键菜单
                    if (e.Button == MouseButtons.Right)
                    {
                        ctxMenuWs.Show(trvRule, e.X, e.Y);
                    }
                    break;

                case "featureclass":
                    tbcFeatCls.BringToFront();
                    ReFreshDataGridFc(e.Node.Tag as DlgTargetFeatureClassBase);
                    //右键菜单
                    if (e.Button == MouseButtons.Right)
                    {
                        ctxMenuFc.Show(trvRule, e.X, e.Y);
                    }
                    break;
            }
        }

        /// <summary>
        /// 刷新方案的信息
        /// </summary>
        /// <param name="dataImportRuleBase"></param>
        private void ReFreshDataGridWs(DataImportRuleBase dataImportRuleBase)
        {
            DataGridViewTextBoxColumn Column1;
            DataGridViewComboBoxColumn Column2;
            ctlImpRule.Rule = dataImportRuleBase as DataImportRule;

            dgvxPipe.Rows.Clear();
            dgvxPipe.Columns.Clear();
            Type type = dataImportRuleBase.GetType();
            List<dynamic> fieldInfos = ObjectConvertor.GetTypeAttributes(type);
            if (fieldInfos == null || fieldInfos.Count == 0) return;            
   


            //如果是管线数据入库 则需要将管线相关的字段信息也显示出来
            if (dataImportRuleBase is PipeImportRule)
            {
                tbiPipeInfo.Visible = true;
                PipeImportRule pipeImportRule = dataImportRuleBase as PipeImportRule;
                PipeFieldSet pipeFieldSet;
                pipeFieldSet = pipeImportRule.pipeFieldSet;
                if (pipeFieldSet == null) return;
                type = pipeFieldSet.GetType();
                fieldInfos = ObjectConvertor.GetTypeAttributes(type);
                foreach (dynamic info in fieldInfos)
                {
                    string name = "";
                    object value = null;
                    if (info is FieldInfo)
                    {
                        name = (info as FieldInfo).Name;
                        value = (info as FieldInfo).GetValue(dataImportRuleBase);
                    }
                    else if (info is PropertyInfo)
                    {
                        name = (info as PropertyInfo).Name;
                        value = (info as PropertyInfo).GetValue(dataImportRuleBase, null);
                    }
                    Column1 = new DataGridViewTextBoxColumn();
                    Column1.Name = name;
                    Column1.HeaderText = XmlHelper.GetXmlAttributeColName(info as MemberInfo);
                    int idx = dgvxPipe.Columns.Add(Column1);
                    if (dgvxPipe.RowCount == 0) dgvxPipe.Rows.Add(1);
                    dgvxPipe.Rows[0].Cells[idx].Value = value;
                }
                dgvxPipe.Rows[0].Tag = pipeFieldSet;

            }
            else
            {
                tbiPipeInfo.Visible = false;
            }
        }

        /// <summary>
        /// 刷新要素类的信息
        /// </summary>
        /// <param name="dlgTargetFeatureClassBase"></param>
        private void ReFreshDataGridFc(DlgTargetFeatureClassBase dlgTargetFeatureClassBase)
        {
            DataGridViewTextBoxColumn Column1;
            DataGridViewComboBoxColumn Column2;
            Type type = dlgTargetFeatureClassBase.GetType();
            List<dynamic> fieldInfos = ObjectConvertor.GetTypeAttributes(type);

            #region 基本信息
            dgvxBasicFc.Rows.Clear();
            dgvxBasicFc.Columns.Clear();
            foreach (dynamic info in fieldInfos)
            {
                bool visable = true;
                string name = "";
                object value = null;
                if (info is FieldInfo)
                {
                    name = (info as FieldInfo).Name;
                    value = (info as FieldInfo).GetValue(dlgTargetFeatureClassBase);
                }
                else if (info is PropertyInfo)
                {
                    name = (info as PropertyInfo).Name;
                    value = (info as PropertyInfo).GetValue(dlgTargetFeatureClassBase, null);
                }
                int idx = 0;

                if ((name.Equals("attrFields", StringComparison.CurrentCultureIgnoreCase)) || name.Equals("featureCodes", StringComparison.CurrentCultureIgnoreCase)) continue;
                if (name.Equals("GeometryType",StringComparison.CurrentCultureIgnoreCase))
                {
                    Column2 = new DataGridViewComboBoxColumn();
                    Column2.HeaderText = XmlHelper.GetXmlAttributeColName(info as MemberInfo);
                    if (dlgTargetFeatureClassBase is DlgTargetFeatureClass || dlgTargetFeatureClassBase is DlgTargetMatchFeatureClass)
                    {
                        Column2.Items.AddRange(new object[]
                                               {
                                                   "点",
                                                   "线",
                                                   "面",
                                                   "注记"   //added by sheng 
                                               });
                    }
                    else
                    {
                        Column2.Items.AddRange(new object[]
                                               {
                                                   "点",
                                                   "线"
                                               });
                    }
                    Column2.Name = "ColumnGeometryType";
                    idx = dgvxBasicFc.Columns.Add(Column2);
                    Column2.Visible = visable;
                }
                else if (name == "HasZ" || name == "HasM")
                {
                    Column2 = new DataGridViewComboBoxColumn();
                    Column2.HeaderText = XmlHelper.GetXmlAttributeColName(info as MemberInfo);
                    Column2.Items.AddRange(new object[]
                                               {
                                                   "True",
                                                   "False" 
                                               });
                    //Column2.Name = "ColumnGeometryType";
                    idx = dgvxBasicFc.Columns.Add(Column2);
                    Column2.Visible = visable;
                }
                else
                {
                    Column1 = new DataGridViewTextBoxColumn();
                    Column1.HeaderText = XmlHelper.GetXmlAttributeColName(info as MemberInfo);
                    //对于List就不显示了
                    if (value is IList)
                    {
                        visable = false;
                    }
                    idx = dgvxBasicFc.Columns.Add(Column1);
                    Column1.Visible = true;
                }
                if (dgvxBasicFc.RowCount == 0) dgvxBasicFc.Rows.Add(1);
                dgvxBasicFc.Rows[0].Cells[idx].Value = value.ToString();
            }
            dgvxBasicFc.Rows[0].Tag = dlgTargetFeatureClassBase;
            #endregion

            #region 代码信息
            if (dlgTargetFeatureClassBase is DlgTargetFeatureClass)
            {
                //if (!tbcBasicFeat.Controls.Contains(tbiFcode))
                //    tbcBasicFeat.Controls.Add(tbiFcode);
                tbiFcode.Visible = true;
                dgvxFcode.Rows.Clear();
                WriteXmlToGrid(dgvxFcode, ((DlgTargetFeatureClass)dlgTargetFeatureClassBase).featureCodes);
            }
            else
            {
                //if (tbcBasicFeat.Controls.Contains(this.tbiFcode))
                //    tbcBasicFeat.Controls.Remove(this.tbiFcode);
                tbiFcode.Visible = false;
            }
            #endregion

            #region 字段信息
            dgvxField.Rows.Clear();
            WriteXmlToGrid(dgvxField, dlgTargetFeatureClassBase.attrFields);
            #endregion

        }

        /// <summary>
        /// 用List填充DataGridView
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="vDataGridView"></param>
        /// <param name="pList"></param>
        private void WriteXmlToGrid<T>(DataGridView vDataGridView, List<T> pList)
        {
            T objT = Activator.CreateInstance<T>();
            
            Type type = objT.GetType();
            List<dynamic> fieldInfos = ObjectConvertor.GetTypeAttributes(type);

            DataGridViewTextBoxColumn Column1;
            DataGridViewComboBoxColumn Column2;

            vDataGridView.Rows.Clear();
            vDataGridView.Columns.Clear();
            foreach (dynamic info in fieldInfos)
            {
                bool visable = true;
                string name = "";
                object value = null;
                if (info is FieldInfo)
                {
                    name = (info as FieldInfo).Name;
                    value = (info as FieldInfo).GetValue(objT);
                }
                else if (info is PropertyInfo)
                {
                    name = (info as PropertyInfo).Name;
                    value = (info as PropertyInfo).GetValue(objT, null);
                }
                int idx = 0;
                //对于List就不显示了
                if (value is IList) continue;
                if (name.ToUpper() == "TYPE") //需要combox
                {
                    Column2 = new DataGridViewComboBoxColumn();
                    Column2.HeaderText = XmlHelper.GetXmlAttributeColName(info as MemberInfo);
                    Column2.Items.AddRange(new object[]
                                                   {
                                                       "整型", "长整型", "字符串型", "双精度型", "单精度型", "时间型","BLOB型"
                                                   });
                    Column2.Name = "ColumnFieldType";
                    idx = vDataGridView.Columns.Add(Column2);
                }
                else
                {
                    Column1 = new DataGridViewTextBoxColumn();
                    //Column1.HeaderText = XmlHelper.GetXmlAttributeColName(info as MemberInfo);
                    string colName = XmlHelper.GetXmlAttributeColName(info as MemberInfo);
                    switch (colName)
                    {
                        case "FCODE":
                            Column1.HeaderText = "要素代码";
                            break;
                        case "FNAME":
                            Column1.HeaderText = "要素名称";
                            break;
                        case "SOURCE":
                            Column1.HeaderText = "要素源代码";
                            break;
                        default:
                            Column1.HeaderText = colName;
                            break;
                    }
                    ////对于List就不显示了
                    //if (fieldInfos[i].GetValue(objT) is IList)
                    //{
                    //    Column1.Visible = false;
                    //}
                    idx = vDataGridView.Columns.Add(Column1);
                }
            }
            if (pList == null) return;
            if (pList.Count > 0)
            {
                vDataGridView.Rows.Add(pList.Count);
                for (int j = 0; j < pList.Count; j++)
                {
                    objT = pList[j];
                    for (int i = 0; i < fieldInfos.Count; i++)
                    {
                        object value = null;
                        if (fieldInfos[i] is FieldInfo)
                        {
                            value = (fieldInfos[i] as FieldInfo).GetValue(objT);
                        }
                        else if (fieldInfos[i] is PropertyInfo)
                        {
                            value = (fieldInfos[i] as PropertyInfo).GetValue(objT, null);
                        }
                        vDataGridView.Rows[j].Cells[i].Value = StringHelper.ConvertWRTNull(value).Trim();
                        vDataGridView.Rows[j].Tag = objT;
                    }
                }
            }
            //else
            //{
            //    vDataGridView.Rows.Add();
            //}
        }

        /// <summary>
        /// 设置进度条最大值
        /// </summary>
        /// <param name="max"></param>
        private void setPgbMax(int max)
        {
            progressBar.Maximum = max;
            progressBar.Minimum = 0;
            progressBar.Value = 0;
            progressBar.Visible = true;
            Application.DoEvents();
        }

        /// <summary>
        /// 设置进度条步进
        /// </summary>
        /// <param name="current"></param>
        private void setPgbCurrent(int current)
        {
            if (current <= progressBar.Maximum)
            {
                progressBar.Value = current;
            }
            else
            {
                progressBar.Value = progressBar.Maximum;
                progressBar.Visible = false;
            }
            Application.DoEvents();
        }

        /// <summary>
        /// 根据方案创建库体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCreateWs_Click(object sender, EventArgs e)
        {
            DataImportRuleBase dataImportRuleBase = null;
            if (trvRule.SelectedNode != null)
            {
                dataImportRuleBase = trvRule.SelectedNode.Tag as DataImportRuleBase;
            }

            if (dataImportRuleBase == null) return;


            //shielded by sheng at 2013-06-20 支持直接创建要素类，无需放在数据集下
            //if (string.IsNullOrEmpty(dataImportRuleBase.DatasetName))
            //{
            //    MsgHelper.ShowInfomationMsgbox("数据集名称不允许为空，请检查！");
            //    return;
            //}

            DlgDataImportConstruct dlgDataImportConstruct = new DlgDataImportConstruct();

            dlgDataImportConstruct.DataImportRuleBase = dataImportRuleBase;
            dlgDataImportConstruct.DataImportRule = dataImportRuleBase as DataImportRule;
            dlgDataImportConstruct.SetProcessBarMax = setPgbMax;
            dlgDataImportConstruct.SetProcessBarCurrent = setPgbCurrent;
            if (dlgDataImportConstruct.CreateTargetFeatClasses())
            {
                MsgHelper.ShowInfomationMsgbox("创建库体成功！");
            }
            else
            {
                MsgHelper.ShowWarningMsgbox("创建库体失败,详情请查看日志！");
            }
        }

        /// <summary>
        /// 类似创建方案
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCreateSameRule_Click(object sender, EventArgs e)
        {
            TreeNode trvNode = trvRule.SelectedNode;

            string key = trvNode.ImageKey;

            DataImportRuleBase newDataImportRuleBase = CreateSameDataImportRule();

            if (newDataImportRuleBase == null) return;

            //重新加载树节点，所有建库方案在树上列出
            ConfigHelper.SaveObject<DataBuilding>(RuleConfig.DataBuilding,"Rule");
            InitTreeViewDataImportRule(trvRule);

            foreach (TreeNode trvNodeRule in trvRule.Nodes)
            {
                foreach (TreeNode trvNodeWs in trvNodeRule.Nodes)
                {
                    if (trvNodeWs.Tag != null && trvNodeWs.Tag is DataImportRuleBase &&
                       (trvNodeWs.Tag as DataImportRuleBase).RuleName == newDataImportRuleBase.RuleName)
                    {
                        trvRule.SelectedNode = trvNodeWs;
                        trvRule_NodeMouseClick(null, new TreeNodeMouseClickEventArgs(trvRule.SelectedNode, 0, 0, 0, 0));
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 类似创建方案
        /// </summary>
        /// <returns></returns>
        private DataImportRuleBase CreateSameDataImportRule()
        {
            //xml节点
            DataImportRuleBase dataImportRuleBase = trvRule.SelectedNode.Tag as DataImportRuleBase;
            if (dataImportRuleBase == null) return null;

            DataImportRuleBase dataImportRuleBaseNew = dataImportRuleBase.Clone() as DataImportRuleBase;
            if (dataImportRuleBaseNew == null) return null;

            List<DataImportRule> dataImportRules = null;
            List<PipeImportRule> pipeImportRules = null;
            List<DataMatchRule> dataMatchRules = null;

            bool sameName = false;
            if (dataImportRuleBaseNew is DataImportRule)
            {
                dataImportRules = RuleConfig.DataBuilding.BuildingRules;
                foreach (DataImportRule rule in dataImportRules)
                {
                    if (rule.RuleName == (dataImportRuleBaseNew.RuleName + "复件"))
                    {
                        sameName = true;
                    }
                }
            }
            else if (dataImportRuleBaseNew is PipeImportRule)
            {
                pipeImportRules = ConvertHelper.DataConvert.PipeImportRules;
                foreach (PipeImportRule rule in pipeImportRules)
                {
                    if (rule.RuleName == (dataImportRuleBaseNew.RuleName + "复件"))
                    {
                        sameName = true;
                    }
                }
            }
            else if (dataImportRuleBase is DataMatchRule)
            {
                dataMatchRules = ConvertHelper.DataConvert.DataMatchtRules;
                foreach (DataMatchRule rule in dataMatchRules)
                {
                    if (rule.RuleName == (dataImportRuleBaseNew.RuleName + "复件"))
                    {
                        sameName = true;
                    }
                }
            }

            if (sameName == false)
            {
                dataImportRuleBaseNew.RuleName = dataImportRuleBase.RuleName + "复件";
                if (dataImportRuleBaseNew is DataImportRule)
                {
                    dataImportRules.Add(dataImportRuleBaseNew as DataImportRule);
                }
                else if (dataImportRuleBaseNew is PipeImportRule)
                {
                    pipeImportRules.Add(dataImportRuleBaseNew as PipeImportRule);
                }
                else if (dataImportRuleBaseNew is DataMatchRule)
                {
                    dataMatchRules.Add(dataImportRuleBaseNew as DataMatchRule);
                }
            }
            else
            {
                MsgHelper.ShowWarningMsgbox("已经存在节点[" + dataImportRuleBase.RuleName + "复件]" + "，请修改节点名称。");
                return null;
            }
            return dataImportRuleBaseNew;
        }

        /// <summary>
        /// 删除方案
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuDeleteRule_Click(object sender, EventArgs e)
        {
            //xml节点
            if (trvRule.SelectedNode.Tag == null || (trvRule.SelectedNode.Tag is DataImportRuleBase) == false) return;

            DataImportRuleBase dataImportRuleBase = trvRule.SelectedNode.Tag as DataImportRuleBase;

            if (dataImportRuleBase is DataImportRule)
            {
                RuleConfig.DataBuilding.BuildingRules.Remove(dataImportRuleBase as DataImportRule);
            }
            else if (dataImportRuleBase is PipeImportRule)
            {
                ConvertHelper.DataConvert.PipeImportRules.Remove(dataImportRuleBase as PipeImportRule);
            }
            else if (dataImportRuleBase is DataMatchRule)
            {
                ConvertHelper.DataConvert.DataMatchtRules.Remove(dataImportRuleBase as DataMatchRule);
            }
            ConfigHelper.SaveObject<DataBuilding>(RuleConfig.DataBuilding,"Rule");

            //重新加载树节点，所有建库方案在树上列出
            trvRule.Nodes.Remove(trvRule.SelectedNode);
            if (trvRule.SelectedNode != null)
            {
                trvRule_NodeMouseClick(null, new TreeNodeMouseClickEventArgs(trvRule.SelectedNode, 0, 0, 0, 0));
            }
        }

        /// <summary>
        /// 增加要素类节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuAddFeatCls_Click(object sender, EventArgs e)
        {
            DataImportRuleBase dataImportRuleBase = trvRule.SelectedNode.Tag as DataImportRuleBase;
            if (dataImportRuleBase == null) return;
            
            featClsIndex = 1;
            
            //判断是否有重名现象
            ExsitFeatClsNode(dataImportRuleBase);

            List<AttrField> attrFields = new List<AttrField>();

            List<FeatureCode> featureCodes = new List<FeatureCode>();

            DlgTargetFeatureClassBase dlgTargetFeatureClassBase = null;
            if (dataImportRuleBase is DataImportRule)
            {
                DlgTargetFeatureClass dlgTargetFeatureClassNew = new DlgTargetFeatureClass();
                dlgTargetFeatureClassNew.FeatureClassName = "新建要素类节点" + featClsIndex.ToString();
                dlgTargetFeatureClassNew.FeatureClassAliasName = "新建要素类节点" + featClsIndex.ToString();
                dlgTargetFeatureClassNew.GeometryType = "点";

                dlgTargetFeatureClassNew.attrFields = attrFields;
                dlgTargetFeatureClassNew.featureCodes = featureCodes;
                ((DataImportRule)dataImportRuleBase).dlgTargetFeatureClasses.Add(dlgTargetFeatureClassNew);
                dlgTargetFeatureClassBase = dlgTargetFeatureClassNew;
            }
            else if (dataImportRuleBase is PipeImportRule)
            {
                DlgTargetPipeFeatureClass dlgTargetPipeFeatureClass = new DlgTargetPipeFeatureClass();
                dlgTargetPipeFeatureClass.FeatureClassName = 
                dlgTargetPipeFeatureClass.FeatureClassAliasName = "新建要素类节点" + featClsIndex.ToString();
                dlgTargetPipeFeatureClass.GeometryType = "点";
                dlgTargetPipeFeatureClass.CorrespondingName = "";
                dlgTargetPipeFeatureClass.PrefixName = "";
                dlgTargetPipeFeatureClass.SuffixName = "Point";
                dlgTargetPipeFeatureClass.attrFields = attrFields;
                ((PipeImportRule)dataImportRuleBase).dlgTargetPipeFeatureClasses.Add(dlgTargetPipeFeatureClass);
                dlgTargetFeatureClassBase = dlgTargetPipeFeatureClass;
            }
            else if (dataImportRuleBase is DataMatchRule)
            {
                DlgTargetMatchFeatureClass dlgTargetMatchFeatureClassNew = new DlgTargetMatchFeatureClass();
                dlgTargetMatchFeatureClassNew.FeatureClassName ="新建要素类节点" + featClsIndex.ToString();
                dlgTargetMatchFeatureClassNew.FeatureClassAliasName ="新建要素类节点" + featClsIndex.ToString();
                dlgTargetMatchFeatureClassNew.GeometryType = "点";
                dlgTargetMatchFeatureClassNew.attrFields = attrFields;

                ((DataMatchRule)dataImportRuleBase).dlgTargetMatchFeatureClass.Add(dlgTargetMatchFeatureClassNew);
                dlgTargetFeatureClassBase = dlgTargetMatchFeatureClassNew;

            }

            ConfigHelper.SaveObject<DataBuilding>(RuleConfig.DataBuilding,"Rule");

            //加载到树节点上
            TreeNode pNodeNew = trvRule.SelectedNode.Nodes.Add(dlgTargetFeatureClassBase.FeatureClassAliasName,
                                                                 dlgTargetFeatureClassBase.FeatureClassAliasName, "featureclass",
                                                                 "featureclass");

            pNodeNew.Tag = dlgTargetFeatureClassBase;
        }

        /// <summary>
        /// 判断新建要素类节点名是否存在，如果存在则往后累加
        /// </summary>
        /// <param name="dataImportRuleBase"></param>
        private void ExsitFeatClsNode(DataImportRuleBase dataImportRuleBase)
        {
            bool exsit = false;
            if (dataImportRuleBase is DataImportRule)
            {
                DlgTargetFeatureClass dlgTargetFeatureClassTemp =
                ListHelper.GetEnumeratorItem(((DataImportRule)dataImportRuleBase).dlgTargetFeatureClasses,
                                             "FeatureClassName", "新建要素类节点" + featClsIndex.ToString());
                if (dlgTargetFeatureClassTemp != null)
                {
                    exsit = true;
                }
            }
            else if (dataImportRuleBase is PipeImportRule)
            {
                DlgTargetPipeFeatureClass dlgTargetPipeFeatureClassTemp =
                ListHelper.GetEnumeratorItem(((PipeImportRule)dataImportRuleBase).dlgTargetPipeFeatureClasses,
                                             "FeatureClassName", "新建要素类节点" + featClsIndex.ToString());
                if (dlgTargetPipeFeatureClassTemp != null)
                {
                    exsit = true;
                }
            }
            else if (dataImportRuleBase is DataMatchRule)
            {
                DlgTargetMatchFeatureClass dlgTargetMatchFeatureClassTemp =
                                    ListHelper.GetEnumeratorItem(((DataMatchRule)dataImportRuleBase).dlgTargetMatchFeatureClass,
                                             "FeatureClassName", "新建要素类节点" + featClsIndex.ToString());
                if (dlgTargetMatchFeatureClassTemp != null)
                {
                    exsit = true;
                }
            }

            if (exsit)
            {
                featClsIndex++;
                ExsitFeatClsNode(dataImportRuleBase);
            }
        }

        /// <summary>
        /// 创建新入库方案
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCreateRule_Click(object sender, EventArgs e)
        {
            DataImportRuleBase newDataImportRuleBase = CreateNewDataImportRule();

            if (newDataImportRuleBase == null) return;

            //重新加载树节点，所有建库方案在树上列出
            ConfigHelper.SaveObject<DataBuilding>(RuleConfig.DataBuilding,"Rule");

            InitTreeViewDataImportRule(trvRule);

            foreach (TreeNode trvNodeRule in trvRule.Nodes)
            {
                foreach (TreeNode trvNodeWs in trvNodeRule.Nodes)
                {
                    if (trvNodeWs.Tag != null && trvNodeWs.Tag is DataImportRuleBase &&
                       (trvNodeWs.Tag as DataImportRuleBase).RuleName == newDataImportRuleBase.RuleName)
                    {
                        trvRule.SelectedNode = trvNodeWs;
                        trvRule_NodeMouseClick(null, new TreeNodeMouseClickEventArgs(trvRule.SelectedNode, 0, 0, 0, 0));
                        return;
                    }
                }
            }
        }
        
        /// <summary>
        /// 创建新入库方案
        /// </summary>
        /// <returns></returns>
        private DataImportRuleBase CreateNewDataImportRule()
        {
            if (trvRule.SelectedNode == null) return null;

            ruleIndex = 1;

            //判断是否有重名现象
            ExsitRuleNode(trvRule.SelectedNode.Text);

            List<DataImportRule> dataImportRules = null;
            List<PipeImportRule> pipeImportRules = null;
            List<DataMatchRule> dataMatchRules = null;

            if (trvRule.SelectedNode.Text == "建库规则")
            {
                dataImportRules = RuleConfig.DataBuilding.BuildingRules;

                DataImportRule dataImportRule = new DataImportRule();
                dataImportRule.RuleName = "新建方案节点" + ruleIndex.ToString();

                dataImportRule.dlgTargetFeatureClasses = new List<DlgTargetFeatureClass>();
                dataImportRule.WorkspaceType = "";
                dataImportRule.IsDefault = false;
                dataImportRule.DatasetName = "";
                dataImportRule.Affix = "";
                dataImportRule.Server = "";
                dataImportRule.Database = "";
                dataImportRule.User = "";
                dataImportRule.Password = "";
                dataImportRule.Path = "";
                dataImportRule.SourceFieldName = "";       

                dataImportRules.Add(dataImportRule);
                return dataImportRule;
            }
            else if (trvRule.SelectedNode.Text == "管线入库")
            {
                pipeImportRules = ConvertHelper.DataConvert.PipeImportRules;

                PipeImportRule pipeImportRule = new PipeImportRule();
                pipeImportRule.RuleName = "新建方案节点" + ruleIndex.ToString();

                pipeImportRule.dlgTargetPipeFeatureClasses = new List<DlgTargetPipeFeatureClass>();
                pipeImportRule.pipeFieldSet = new PipeFieldSet();

                pipeImportRules.Add(pipeImportRule);
                return pipeImportRule;
            }
            else if (trvRule.SelectedNode.Text == "按图层入库")
            {
                dataMatchRules = ConvertHelper.DataConvert.DataMatchtRules;

                DataMatchRule dataMatchRule = new DataMatchRule();
                dataMatchRule.RuleName = "新建方案节点" + ruleIndex.ToString();

                dataMatchRule.dlgTargetMatchFeatureClass = new List<DlgTargetMatchFeatureClass>();

                dataMatchRules.Add(dataMatchRule);
                return dataMatchRule;
            }

            return null;
        }

        /// <summary>
        /// 判断新建方案节点名是否存在，如果存在则往后累加
        /// </summary>
        /// <param name="ruleType"></param>
        private void ExsitRuleNode(string ruleType)
        {
            bool exsit = false;
            if (ruleType == "建库规则")
            {
                DataImportRule dataImportRule =
                ListHelper.GetEnumeratorItem(RuleConfig.DataBuilding.BuildingRules,
                                             "RuleName", "新建方案节点" + ruleIndex.ToString());
                if (dataImportRule != null)
                {
                    exsit = true;
                }
            }
            else if (ruleType == "按图层入库")
            {
                DataMatchRule dataMatchRule =
                ListHelper.GetEnumeratorItem(ConvertHelper.DataConvert.DataMatchtRules,
                                             "RuleName", "新建方案节点" + ruleIndex.ToString());
                if (dataMatchRule != null)
                {
                    exsit = true;
                }
            }
            else if (ruleType == "管线入库")
            {
                PipeImportRule pipeImportRule =
                ListHelper.GetEnumeratorItem(ConvertHelper.DataConvert.PipeImportRules,
                                             "RuleName", "新建方案节点" + ruleIndex.ToString());
                if (pipeImportRule != null)
                {
                    exsit = true;
                }
            }

            if (exsit)
            {
                ruleIndex++;
                ExsitRuleNode(ruleType);
            }
        }

        /// <summary>
        /// 删除要素类节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuDeleteFc_Click(object sender, EventArgs e)
        {
            //xml节点
            DlgTargetFeatureClassBase dlgTargetFeatureClassBase = trvRule.SelectedNode.Tag as DlgTargetFeatureClassBase;
            if (dlgTargetFeatureClassBase == null) return;

            DataImportRuleBase dataImportRuleBase = trvRule.SelectedNode.Parent.Tag as DataImportRuleBase;

            if (dataImportRuleBase is DataImportRule)
            {
                ((DataImportRule)dataImportRuleBase).dlgTargetFeatureClasses.Remove(dlgTargetFeatureClassBase as DlgTargetFeatureClass);
            }
            else if (dataImportRuleBase is PipeImportRule)
            {
                ((PipeImportRule)dataImportRuleBase).dlgTargetPipeFeatureClasses.Remove(dlgTargetFeatureClassBase as DlgTargetPipeFeatureClass);
            }
            else if (dataImportRuleBase is DataMatchRule)
            {
                ((DataMatchRule)dataImportRuleBase).dlgTargetMatchFeatureClass.Remove(dlgTargetFeatureClassBase as DlgTargetMatchFeatureClass);
            }
            ConfigHelper.SaveObject<DataBuilding>(RuleConfig.DataBuilding,"Rule");

            //修改树节点
            trvRule.Nodes.Remove(trvRule.SelectedNode);
            if (trvRule.SelectedNode != null)
            {
                trvRule_NodeMouseClick(null, new TreeNodeMouseClickEventArgs(trvRule.SelectedNode, 0, 0, 0, 0));
            }
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            if (trvRule.SelectedNode == null)
            {
                MsgHelper.ShowInfomationMsgbox("请选中需要刷新的建库方案节点或者要素类方案节点！");
                return;
            }
            switch (trvRule.SelectedNode.ImageKey)
            {
                case "workspace":
                    ReFreshDataGridWs(trvRule.SelectedNode.Tag as DataImportRuleBase);
                    break;
                case "featureclass":
                    ReFreshDataGridFc(trvRule.SelectedNode.Tag as DlgTargetFeatureClassBase);
                    break;
                default :
                    MsgHelper.ShowInfomationMsgbox("请选中需要刷新的建库方案节点或者要素类方案节点！");
                    return;
            }
        }

        /// <summary>
        /// 应用所有修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonApply_Click(object sender, EventArgs e)
        {
            if (trvRule.SelectedNode == null)
            {
                MsgHelper.ShowInfomationMsgbox("请首先选中树节点！");
                return;
            }

            //首先让DataGrid失去焦点
            trvRule.Focus();

            Type type = null ;
            Type typeNew = null;
            PropertyInfo[] propertyInfos;
            PropertyInfo[] propertyInfosNew;

            DataImportRuleBase dataImportRuleBase = null;
            DataImportRule dataImportRuleBaseNew = ctlImpRule.Rule;
            
            switch (trvRule.SelectedNode.ImageKey)
            {
                case "workspace":
                    #region 修改库体规则信息
                    dataImportRuleBase = trvRule.SelectedNode.Tag as DataImportRuleBase;
                    if (dataImportRuleBase == null) return;

                    switch (tbcWorkspace.SelectedTab.Name)
                    {
                        case "tbiWsBasic":
                            //dgvX = dgvxBasicWs;
                            type = dataImportRuleBase.GetType();
                            typeNew = dataImportRuleBaseNew.GetType();
                            break;

                        case "tbiPipeInfo":
                            if (!(dataImportRuleBase is PipeImportRule)) return;
                            type = (dataImportRuleBase as PipeImportRule).pipeFieldSet.GetType();
                            break;
                    }
                    if (type == null || typeNew == null) return;
                    propertyInfos = type.GetProperties();
                    propertyInfosNew = typeNew.GetProperties();
                    for (int i = 0; i < propertyInfosNew.Length; i++)
                    {
                        if (propertyInfosNew[i].PropertyType != typeof(string)) continue;
                        for (int j = 0; j < propertyInfos.Length; j++)
                        {
                            if (propertyInfos[j].Name == propertyInfosNew[i].Name && propertyInfosNew[i].GetValue(dataImportRuleBaseNew, null)!=null)
                            {
                                string afterapply = propertyInfosNew[i].GetValue(dataImportRuleBaseNew,null).ToString();
                                propertyInfos[j].SetValue(dataImportRuleBase, afterapply, null);
                                break;
                            }
                        }
                    }

                    if (dataImportRuleBase.RuleName == null || dataImportRuleBase.RuleName.Trim() == "")
                    {
                        MsgHelper.ShowInfomationMsgbox("方案名称不允许为空，请修改！");
                        return;
                    }

                    if (dataImportRuleBase is DataImportRule)
                    {
                        List<DataImportRule> temp = ListHelper.GetEnumeratorItems(RuleConfig.DataBuilding.BuildingRules,
                                                                       "RuleName", dataImportRuleBase.RuleName);
                        if (temp.Count > 1)
                        {
                            MsgHelper.ShowInfomationMsgbox("方案名称重复，请修改！");
                            dataImportRuleBase.RuleName = trvRule.SelectedNode.Text;
                            return;
                        }
                    }
                    else if (dataImportRuleBase is PipeImportRule)
                    {
                        List<PipeImportRule> temp = ListHelper.GetEnumeratorItems(ConvertHelper.DataConvert.PipeImportRules,
                                                                       "RuleName", dataImportRuleBase.RuleName);
                        if (temp.Count > 1)
                        {
                            MsgHelper.ShowInfomationMsgbox("方案名称重复，请修改！");
                            dataImportRuleBase.RuleName = trvRule.SelectedNode.Text;
                            return;
                        }
                    }
                    else if (dataImportRuleBase is DataMatchRule)
                    {
                        List<DataMatchRule> temp = ListHelper.GetEnumeratorItems(ConvertHelper.DataConvert.DataMatchtRules,
                                               "RuleName", dataImportRuleBase.RuleName);
                        if (temp.Count > 1)
                        {
                            MsgHelper.ShowInfomationMsgbox("方案名称重复，请修改！");
                            dataImportRuleBase.RuleName = trvRule.SelectedNode.Text;
                            return;
                        }
                    }

                    trvRule.SelectedNode.Text = StringHelper.ConvertWRTNull(dataImportRuleBase.RuleName);
                    ConfigHelper.SaveObject<DataBuilding>(RuleConfig.DataBuilding,"Rule");
                    break;
                    #endregion

                case "featureclass":
                    #region 修改要素类规则信息
                    DlgTargetFeatureClassBase dlgTargetFeatureClassBase = trvRule.SelectedNode.Tag as DlgTargetFeatureClassBase;
                    dataImportRuleBase = trvRule.SelectedNode.Parent.Tag as DataImportRuleBase;

                    if (dataImportRuleBase == null || dlgTargetFeatureClassBase == null) return;

                    Type typeFeatCls = dlgTargetFeatureClassBase.GetType();
                    PropertyInfo[] featClsPropertyInfos = typeFeatCls.GetProperties();

                    switch (tbcFeatCls.SelectedTab.Name)
                    {
                        case "tbiFcBasic":
                            #region 要素类基本信息
                            for (int i = 0; i < dgvxBasicFc.ColumnCount; i++)
                            {
                                if (dgvxBasicFc.Columns[i].Visible)
                                {
                                    for (int j = 0; j < featClsPropertyInfos.GetLength(0); j++)
                                    {
                                        if (XmlHelper.GetXmlAttributeColName(featClsPropertyInfos[j]) == dgvxBasicFc.Columns[i].HeaderText)
                                        {
                                            if (featClsPropertyInfos[j].PropertyType == typeof(Boolean))
                                            {
                                                featClsPropertyInfos[j].SetValue(dlgTargetFeatureClassBase, Boolean.Parse(StringHelper.ConvertWRTNull(dgvxBasicFc.Rows[0].Cells[i].Value).Trim()),null);
                                            }
                                            else if (featClsPropertyInfos[j].PropertyType == typeof(String))
                                            {
                                                featClsPropertyInfos[j].SetValue(dlgTargetFeatureClassBase, StringHelper.ConvertWRTNull(dgvxBasicFc.Rows[0].Cells[i].Value).Trim(),null);
                                            }
                                        }
                                    }
                                }
                            }

                            if (dlgTargetFeatureClassBase.FeatureClassName  == "")
                            {
                                MsgHelper.ShowInfomationMsgbox("要素类名称不能为空！");
                                return;
                            }

                            if (dlgTargetFeatureClassBase.FeatureClassAliasName == "")
                            {
                                MsgHelper.ShowInfomationMsgbox("要素类别名不能为空！");
                                return;
                            }

                            if (dataImportRuleBase is DataImportRule)
                            {
                                List<DlgTargetFeatureClass> dlgTargetFeatureClassesTemp = ListHelper.GetEnumeratorItems(((DataImportRule)dataImportRuleBase).dlgTargetFeatureClasses,
                                                                       "FeatureClassName", dlgTargetFeatureClassBase.FeatureClassName);
                                if (dlgTargetFeatureClassesTemp.Count > 1)
                                {
                                    MsgHelper.ShowInfomationMsgbox("要素类名称[" + dlgTargetFeatureClassBase.FeatureClassName + "]已重复，请修改！");
                                    return;
                                }
                                List<DlgTargetFeatureClass> dlgTargetFeatureClassesTemp2 = ListHelper.GetEnumeratorItems(((DataImportRule)dataImportRuleBase).dlgTargetFeatureClasses,
                                                                           "FeatureClassAliasName", dlgTargetFeatureClassBase.FeatureClassAliasName);
                                if (dlgTargetFeatureClassesTemp2.Count > 1)
                                {
                                    MsgHelper.ShowInfomationMsgbox("要素类别名[" + dlgTargetFeatureClassBase.FeatureClassAliasName + "]已重复，请修改！");
                                    dlgTargetFeatureClassBase.FeatureClassAliasName = trvRule.SelectedNode.Text;
                                    return;
                                }

                                trvRule.SelectedNode.Text = StringHelper.ConvertWRTNull(dlgTargetFeatureClassBase.FeatureClassAliasName).Trim();
                            }
                            else if (dataImportRuleBase is PipeImportRule)
                            {
                                List<DlgTargetPipeFeatureClass> dlgTargetFeatureClassesTemp = ListHelper.GetEnumeratorItems(((PipeImportRule)dataImportRuleBase).dlgTargetPipeFeatureClasses,
                                       "FeatureClassName", dlgTargetFeatureClassBase.FeatureClassName);
                                if (dlgTargetFeatureClassesTemp.Count > 1)
                                {
                                    MsgHelper.ShowInfomationMsgbox("要素类名称[" + dlgTargetFeatureClassBase.FeatureClassName + "]已重复，请修改！");
                                    return;
                                }
                                List<DlgTargetPipeFeatureClass> dlgTargetFeatureClassesTemp2 = ListHelper.GetEnumeratorItems(((PipeImportRule)dataImportRuleBase).dlgTargetPipeFeatureClasses,
                                                                           "FeatureClassAliasName", dlgTargetFeatureClassBase.FeatureClassAliasName);
                                if (dlgTargetFeatureClassesTemp2.Count > 1)
                                {
                                    MsgHelper.ShowInfomationMsgbox("要素类别名[" + dlgTargetFeatureClassBase.FeatureClassAliasName + "]已重复，请修改！");
                                    dlgTargetFeatureClassBase.FeatureClassAliasName = trvRule.SelectedNode.Text;
                                    return;
                                }

                                trvRule.SelectedNode.Text = StringHelper.ConvertWRTNull(dlgTargetFeatureClassBase.FeatureClassAliasName).Trim();
                            }
                            else if (dataImportRuleBase is DataMatchRule)
                            {
                                List<DlgTargetMatchFeatureClass> dlgTargetFeatureClassesTemp = ListHelper.GetEnumeratorItems(((DataMatchRule)dataImportRuleBase).dlgTargetMatchFeatureClass,
                                    "FeatureClassName", dlgTargetFeatureClassBase.FeatureClassName);
                                if (dlgTargetFeatureClassesTemp.Count > 1)
                                {
                                    MsgHelper.ShowInfomationMsgbox("要素类名称[" + dlgTargetFeatureClassBase.FeatureClassName + "]已重复，请修改！");
                                    return;
                                }
                                List<DlgTargetMatchFeatureClass> dlgTargetFeatureClassesTemp2 = ListHelper.GetEnumeratorItems(((DataMatchRule)dataImportRuleBase).dlgTargetMatchFeatureClass,
                                                                           "FeatureClassAliasName", dlgTargetFeatureClassBase.FeatureClassAliasName);
                                if (dlgTargetFeatureClassesTemp2.Count > 1)
                                {
                                    MsgHelper.ShowInfomationMsgbox("要素类别名[" + dlgTargetFeatureClassBase.FeatureClassAliasName + "]已重复，请修改！");
                                    dlgTargetFeatureClassBase.FeatureClassAliasName = trvRule.SelectedNode.Text;
                                    return;
                                }

                                trvRule.SelectedNode.Text = StringHelper.ConvertWRTNull(dlgTargetFeatureClassBase.FeatureClassName).Trim();
                            }

                            //trvRule.SelectedNode.Text = StringUtility.ConvertWRTNull(dlgTargetFeatureClassBase.FeatureClassAliasName).Trim();
                            break;
                            #endregion

                        case "tbiFcode":
                            #region 代码对应关系
                            List<FeatureCode> featureCodes = ((DlgTargetFeatureClass)dlgTargetFeatureClassBase).featureCodes;
                            featureCodes.Clear();
                            if (dgvxFcode.Rows.Count > 0)
                            {
                                for (int i = 0; i < dgvxFcode.Rows.Count - 1; i++)
                                {
                                    FeatureCode featureCode = new FeatureCode();
                                    Type typeFeatCode = featureCode.GetType();
                                    PropertyInfo[] featCodePropertyInfos = typeFeatCode.GetProperties();
                                    for (int j = 0; j < dgvxFcode.ColumnCount; j++)
                                    {
                                        if (dgvxFcode.Columns[j].Visible == true)
                                        {
                                            if (dgvxFcode.Rows[i].Cells[0].Value == null ||
                                                dgvxFcode.Rows[i].Cells[0].Value.ToString().Trim() == "")
                                            {
                                                MsgHelper.ShowInfomationMsgbox("第" + (i + 1).ToString() + "行分类代码为空，请修改！");
                                                return;
                                            }
                                            featCodePropertyInfos[j].SetValue(featureCode,
                                                                           StringHelper.ConvertWRTNull(
                                                                               dgvxFcode.Rows[i].Cells[j].Value).Trim(),null);
                                        }
                                    }
                                    dgvxFcode.Rows[i].Tag = featureCode;
                                    featureCodes.Add(featureCode);
                                }
                            }
                            break;
                            #endregion

                        case "tbiField":
                            #region 字段对应关系
                            List<AttrField> attrFields = dlgTargetFeatureClassBase.attrFields;
                            attrFields.Clear();
                            
                            if (dgvxField.Rows.Count > 0)
                            {
                                List<string> fieldNames = new List<string>();
                                for (int i = 0; i < dgvxField.Rows.Count - 1; i++)
                                {
                                    AttrField attrField = new AttrField();
                                    Type typeAttrField = attrField.GetType();
                                    PropertyInfo[] attrPropertyInfos = typeAttrField.GetProperties();
                                    for (int j = 0; j < dgvxField.ColumnCount; j++)
                                    {
                                        if (dgvxField.Columns[j].Visible == true)
                                        {
                                            if (dgvxField.Rows[i].Cells[0].Value == null ||
                                                dgvxField.Rows[i].Cells[0].Value.ToString().Trim() == "")
                                            {
                                                MsgHelper.ShowInfomationMsgbox("第" + (i + 1).ToString() + "行目标属性字段为空，请修改！");
                                                return;
                                            }
                                            attrPropertyInfos[j].SetValue(attrField,
                                                                       StringHelper.ConvertWRTNull(
                                                                           dgvxField.Rows[i].Cells[j].Value).Trim(),null);
                                        }
                                    }
                                    if (!fieldNames.Contains(dgvxField.Rows[i].Cells[0].Value.ToString().Trim()))
                                    {
                                        fieldNames.Add(dgvxField.Rows[i].Cells[0].Value.ToString().Trim());
                                    }
                                    else
                                    {
                                        MsgHelper.ShowInfomationMsgbox("字段[" + dgvxField.Rows[i].Cells[0].Value.ToString().Trim() + "]重复定义，请修改！");
                                        return;
                                    }

                                    dgvxField.Rows[i].Tag = attrField;
                                    attrFields.Add(attrField);
                                }
                                fieldNames = null;
                            }
                            break;
                            #endregion
                    }
                    ConfigHelper.SaveObject<DataBuilding>(RuleConfig.DataBuilding,"Rule");
                    break;
                    #endregion
            }

            MsgHelper.ShowInfomationMsgbox("修改成功！");
        }

        /// <summary>
        /// 导入方案规则文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonImportRule_Click(object sender, EventArgs e)
        {
            openFileDialog1.Reset();
            openFileDialog1.Title = "导入方案规则XML文件";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter = "方案规则文件(*.xml)|*.xml";

            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                if (openFileDialog1.FileName == "") return;
                try
                {
                    RuleConfig.ReadRules(openFileDialog1.FileName);
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(ex);
                    MsgHelper.ShowInfomationMsgbox("规则格式不符合要求,请与系统管理员联系！");
                    RuleConfig.ReadRules(System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\Config\\规则.xml");
                    return;
                }
                ConfigHelper.SaveObject<DataBuilding>(RuleConfig.DataBuilding,"Rule");
                InitTreeViewDataImportRule(trvRule);
                if (trvRule.Nodes.Count > 0)
                    trvRule_NodeMouseClick(null, new TreeNodeMouseClickEventArgs(trvRule.Nodes[0], 0, 0, 0, 0));
                MsgHelper.ShowInfomationMsgbox("导入成功！");
            }
        }

        /// <summary>
        /// 导出为方案规则文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonExportRule_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "导出方案规则XML文件";
            saveFileDialog1.Filter = "方案规则文件(*.xml)|*.xml";

            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                if (saveFileDialog1.FileName == "") return;
                RuleConfig.WriteRules(saveFileDialog1.FileName);

                MsgHelper.ShowInfomationMsgbox("导出成功！");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmDataImportRule_Resize(object sender, EventArgs e)
        {
            splitContainer3.SplitterDistance = 25;
        }

        private void FrmDataImportRule_Load(object sender, EventArgs e)
        {
            if (trvRule.TopNode.Nodes != null && trvRule.TopNode.Nodes.Count > 0)
            {
                trvRule.SelectedNode = trvRule.TopNode.Nodes[0];
                trvRule_NodeMouseClick(null, new TreeNodeMouseClickEventArgs(trvRule.SelectedNode, 0, 0, 0, 0));
            }
        }
    }
}
